package com.halflife.parameter;

import com.halflife.comeElectricity.basic.datadic.DataDic;
import com.halflife.comeElectricity.basic.datadic.DataDicRepository;
import com.halflife.framework.converter.MapHandler;
import com.halflife.framework.domain.Page;
import com.halflife.framework.utils.ImageUploadUtil;
import com.halflife.parameter.icon.Icon;
import com.halflife.parameter.icon.IconRepository;
import com.halflife.parameter.template.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/front/parameter/")
public class ParameterFrontController {

    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Resource
    private IconRepository iconRepository;
    @Resource
    private ParameterRepository parameterRepository;
    @Resource
    private DataDicRepository dataDicRepository;
    @Resource
    private TemplateRepository templateRepository;

    @RequestMapping("/icon/list")
    public Map<String, List<Icon>> getIconList(HttpServletResponse response,
                           HttpServletRequest request, Model model) {
        Map<String, Object> params = new HashMap<>();
        params.put("valid", true);
        List<Icon> list = iconRepository.selectAll(params);
        Map<String, List<Icon>> icons = new HashMap<>();
        for (Icon icon : list) {
            if (icons.containsKey(icon.getCatagory())) {
                icons.get(icon.getCatagory()).add(icon);
            } else {
                List<Icon> tempList = new ArrayList<>();
                tempList.add(icon);
                icons.put(icon.getCatagory(), tempList);
            }
        }
        return icons;
    }
    @RequestMapping("/parameters/{id}")
    public List<ParameterJson> getParameters(@PathVariable()Integer id, HttpServletResponse response,
                                         HttpServletRequest request, Model model) {
        Map<String, Object> params = new HashMap<>();
        params.put("templateId", id);
        params.put("valid", true);
        List<ParameterJson> list = new ArrayList<>();
        List<Parameter> lists = parameterRepository.selectAll(params);
        for (Parameter parameter : lists) {
            ParameterJson json = new ParameterJson();
            json.parseParameter(parameter);
            list.add(json);
        }
        return list;
    }
    @PostMapping("/parameters/{id}")
    public ResponseEntity saveParameters(@PathVariable()Integer id, @RequestBody() List<ParameterJson> parameters, HttpServletResponse response,
                                         HttpServletRequest request, Model model) {
        Map<String, Object> params = new HashMap<>();
        parameterRepository.update("update t_parameter_parameters t set t.valid=0 where t.templateId="+id);
        int lightCount = 0;
        int propsCount = 0;
        Map<String, Object> param = new HashMap<>();
        param.put("catagory", "Props");
        List<Icon> props = iconRepository.selectAll(param);
        param.remove("catagory");
        param.put("endSql", " t.catagory in ('Lighting Sources')");
        List<Icon> lights = iconRepository.selectAll(param);
        Set<Integer> propSet = new HashSet<>();
        Set<Integer> lightSet = new HashSet<>();
        for (Icon icon : props) {
            propSet.add(icon.getId());
        }
        for (Icon icon : lights) {
            lightSet.add(icon.getId());
        }
        for (ParameterJson json : parameters) {
            if (json.getIconId() != null) {
                if (propSet.contains(json.getIconId())) {
                    propsCount++;
                    continue;
                }
                if (lightSet.contains(json.getIconId())) {
                    lightCount++;
                    continue;
                }
            }
            Parameter parameter = json.convertToBo();
            parameter.setTemplateId(id);
            if (parameter.getId() == null) {
                parameterRepository.insert(parameter);
            } else {
                parameterRepository.updateByPrimaryKeySelective(parameter);
            }

        }
        Template template = new Template();
        template.setId(id);
        template.setLightCount(lightCount);
        template.setPropsCount(propsCount);
        templateRepository.updateByPrimaryKeySelective(template);
        return new ResponseEntity(HttpStatus.OK);
    }
    @RequestMapping("/dataDic/list/{id}")
    public List<DataDic> getIconCatagoryList(@PathVariable(name = "id") Integer id, HttpServletResponse response,
                           HttpServletRequest request, Model model) {
        Map<String, Object> params = new HashMap<>();
        params.put("dataDicTypeId", id);
        params.put("valid", true);
        List<DataDic> list = dataDicRepository.selectAll(params);
        return list;
    }
}
